package cn.com.chec.en.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.controller.BaseMultiActionController;
import cn.com.chec.en.dao.LanguageInfoDao;
import cn.com.chec.en.model.domain.LanguageInfo;

public class LanuageInfoAdminController extends BaseMultiActionController {
	protected static final Logger logger = Logger
			.getLogger(LanuageInfoAdminController.class);
	private LanguageInfoDao languageInfoDao;

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<LanguageInfo> list = this.languageInfoDao.getAll();
		this.map.put("languageInfoList", list);
		return new ModelAndView("admin/languageInfo/list", this.map);
	}

	public ModelAndView showInsert(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		this.viewName = "admin/languageInfo/languageInfoOpr";
		LanguageInfo languageInfo = new LanguageInfo();
		this.map.put("languageInfo", languageInfo);
		this.map.put("act", "insert");
		return new ModelAndView(this.viewName, this.map);
	}

	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String message = "";
		String name = request.getParameter("name");
		String desc = request.getParameter("desc");
		LanguageInfo info = new LanguageInfo();
		info.setName(name);
		info.setDesc(desc);
		if (this.languageInfoDao.insert(info))
			message = "添加成功!";
		else {
			message = "添加失败!";
		}
		this.map.put("message", message);
		return new ModelAndView("admin/common/message", this.map);
	}

	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int languageId = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.parseInt(request.getParameter("id")) : 0;
		LanguageInfo languageInfo = this.languageInfoDao
				.getLanguageInfoById(languageId);
		this.map.put("languageInfo", languageInfo);
		this.map.put("act", "update");
		return new ModelAndView("admin/languageInfo/languageInfoOpr", this.map);
	}

	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String message = "";
		int languageId = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.parseInt(request.getParameter("id")) : 0;
		String desc = request.getParameter("desc");
		LanguageInfo languageInfo = this.languageInfoDao
				.getLanguageInfoById(languageId);
		languageInfo.setDesc(desc);
		if (this.languageInfoDao.update(languageInfo))
			message = "添加成功!";
		else {
			message = "添加成功!";
		}
		this.map.put("message", message);
		return new ModelAndView("admin/common/message", this.map);
	}

	public void setLanguageInfoDao(LanguageInfoDao languageInfoDao) {
		this.languageInfoDao = languageInfoDao;
	}
}